<?php
namespace Ader\Controller;

class IndexController extends Controller {
    
	public function index() {
		$m = D2();
		$page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();
		$this->assign('page',$page);
        $this->display();
    }
	
	
    public function doPasswd() {
        $oldPasswd = I('old_passwd' , '');
        $passwd    = I('passwd'     , '');
        $uid       = C('RUN_LOGIN_USER.uid');
        $m         = D2('User');

        try {
            $r = $m->changePasswd($uid, $oldPasswd, $passwd)
                 ? array('s' => true, 'm' => '密码修改成功')
                 : array('s' => false, 'm' => '密码修改出错');
        } catch(Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }

    public function getUserInfo() {
        $uid = C('RUN_LOGIN_USER.uid');

        if(!$record = D2('User')->getByUid($uid)) {
            showJson(array('s' => false, 'm' => '没有找到会员信息'));
        }

        $field = array('user_name', 'qq', 'email', 'tel', 'reg_time');
        $r     = array();

        foreach($field as $v) {
            $r[$v] = $record[$v];
        }

        $r['bank2'] = getBankNameCn($r['bank']);

        showJson(array('s' => true, 'd' => $r));
    }
	
	public function myInfo() {
        $uid = C('RUN_LOGIN_USER.uid');

        if(!$record = D2('User')->getByUid($uid)) {
            showJson(array('s' => false, 'm' => '没有找到会员信息'));
        }

        $field = array('name', 'qq', 'email', 'tel', 'reg_time');
        $r     = array();

        foreach($field as $v) {
            $r[$v] = $record[$v];
        }

        $r['bank2'] = getBankNameCn($r['bank']);
		$this->assign('page',$r);
		$this->display();
        //showJson(array('s' => true, 'd' => $r));
    }

    public function getLast7DayCost() {
        $m   = D2('VPlanStats');
        $tmp = array();
        $x   = array();
        $t   = array();   //所有的时间戳

        for($i = 1; $i < 8; $i++) {
            $timestamp = strtotime("-{$i} day");
            $day       = date('Y-m-d', $timestamp);
            $tmp2      = $m->field('plan_name, (num + deduction) AS counter')->where(array('day' => $day))->select();

            $tmp[$timestamp] = $tmp2;
            $x[]             = date('m-d', $timestamp);
            $t[]             = $timestamp;
        }

        $tmp2     = array();
        $r        = array();
        $planName = array();

        foreach($tmp as $k => $v) {
            if(empty($v)) {
                continue;
            }

            foreach($v as $v2) {
                $_key = md5($v2['plan_name']);

                if(!array_key_exists($_key, $r)) {
                    $r[$_key] = array('name' => $v2['plan_name']);
                    $planName[$_key] = $v2['plan_name'];
                }

                $r[$_key]['data'][$k] = $v2['counter'];
            }
        }

        /**
         * 所有计划按日期都补全结算数
         */
        foreach($planName as $k => $v) {
            foreach($r as $k2 => $v2) {
                if($k == $k2) {
                    foreach($t as $k3 => $v3) {
                        if(!array_key_exists($v3, $v2['data'])) {
                            $r[$k2]['data'][$v3] = 0;
                        }
                    }
                }
            }

            $_key = md5($v);

            if(empty($r[$_key])) {
                foreach($t as $v3) {
                    $r[$_key]['data'][] = 0;
                }
            }
        }

        $r2 = array();

        /**
         * 将部分下标改成从0开始的数字下标
         */
        foreach($r as $v) {
            $r2[] = array(
                'name' => $v['name'],
                'data' => array_values($v['data']),
            );
        }


        $r3 = array(
            'x' => $x,
            'd' => $r2
        );

        showJson(array('s' => true, 'd' => $r3));
    }

}
